import { AnimationTask } from "@/lib/y-animation";
import { IElement, IMap, IMapInfo } from "./useMap";

const getCoordinateY = (y: number, height: number, info: IMapInfo): number => {
  return info.mapHeight - y + info.floorHeight - height;
}


const useHuman = (name: string,  info: any = {}): IHuman => {
  const height = info.height || 175;
  const width = info.width || 80;
  const x = info.x || 400;
  const y = info.y || 5;
  const animationTask: AnimationTask = info.animationTask;
  const mapInfo = info.mapInfo;


  const jump = () => {
    console.log('jump');
    animationTask.createAni(
      {
        styleName: 'translateY',
        domID: name,
        to: getCoordinateY(y + 200, height, mapInfo),
        eventTime: 1000,
      },
      {
        callback: () => {
          animationTask.createAni(
            {
              styleName: 'translateY',
              domID: name,
              to: getCoordinateY(y, height, mapInfo),
              eventTime: 1000,
            },
            {
              from: getCoordinateY(y + 200, height, mapInfo),
              type: 'ease-out'
            }
          )
        },
        from: getCoordinateY(y, height, mapInfo),
        type: 'ease-out'
      }
    )
  };

  const leftMove = () => {
    const next = human.x - 50;
    animationTask.createAni(
      {
        styleName: 'translateX',
        domID: name,
        to: next,
        eventTime: 400,
      },
      {
        from: human.x,
      }
    )
    human.x = next;
  };

  const rightMove = () => {
    const next = human.x + 50;
    animationTask.createAni(
      {
        styleName: 'translateX',
        domID: name,
        to: next,
        eventTime: 400,
      },
      {
        from: human.x
      }
    )
    human.x = next;
  };
  
  const shooting = () => {

  }


  const human = {
    domID: name,
    x,
    y,
    height,
    width,
    name,
    jump,
    leftMove,
    rightMove,
    shooting,
  }

  return human;
}

export default useHuman;

export interface IHuman extends IElement {
  name: string;
  jump: () => void;
  leftMove: () => void;
  rightMove: () => void;
  shooting: () => void;
}

export type TMoveType = 'left' | 'right'